/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.dusa.worklog.datalib;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

/**
 * This class encapsulates one project, i.e. a separated work at some customer.
 * Each project has a link to a customer for that the work is made. The project
 * contains a list of work days.
 * @author vdusa
 * @see WorkDay
 * @see Customer
 */
@Entity
@Table(name = "PROJECT", schema = "APP")
@NamedQueries({
@NamedQuery(name="getAllProjects",query="SELECT p FROM Project p")
})
public class Project implements Serializable, WorklogData {

    @OneToMany(mappedBy = "project", cascade={CascadeType.REMOVE, CascadeType.REFRESH, CascadeType.MERGE})
    @OrderBy("workDate")
    private List<WorkDay> workDays = new ArrayList<WorkDay>();
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    @ManyToOne(cascade={CascadeType.REMOVE, CascadeType.REFRESH})
    private Customer customer;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Project)) {
            return false;
        }
        Project other = (Project) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getName();
    }

    /**
     * @return the customer for that the project is performed
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the name of the project
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the work days that have been performed during this project
     */
    public List<WorkDay> getWorkDays() {
        return workDays;
    }

    /**
     * @param workDays the workDays to set
     */
    public void setWorkDays(List<WorkDay> workDays) {
        this.workDays = workDays;
    }

    public double getTotalHours(){
        double total = 0;
        
        for(WorkDay w: getWorkDays()){
            total += w.getTotalWork();
        }

        return total;
    }
}
